package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.platform.PlatformUpDto;
import com.frejoys.common.util.RedisUtil;
import com.frejoys.common.vo.admin.PlatformVo;
import com.frejoys.dao.entity.Platform;
import com.frejoys.dao.entity.Verify;
import com.frejoys.dao.mapper.PlatformMapper;
import com.frejoys.dao.mapper.VerifyMapper;
import com.frejoys.service.BlackService;
import com.frejoys.service.PlatformService;
import com.frejoys.service.VerifyService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

// 平台设置
@Service
@RequiredArgsConstructor
public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform> implements PlatformService {

    private final RedisUtil redisUtil;

    private final BlackService blackService;

    // 表id
    private Integer id = 1;

    @Override
    @Cacheable(cacheNames = "platform", key = "#root.method.name", unless = "#result == null")
    public Platform getInfo() {
        LambdaQueryWrapper<Platform> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Platform::getId, id);
        return getOnly(wrapper);
    }


    @Override
    public boolean upPlatform(PlatformUpDto dto) {
        LambdaUpdateWrapper<Platform> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Platform::getId,dto.getId());

        if (dto.getStoreCashTime() != null){
            wrapper.set(Platform::getStoreCashTime,dto.getStoreCashTime());
        }
        if (dto.getUserCashTime() != null){
            wrapper.set(Platform::getUserCashTime,dto.getUserCashTime());
        }
        if (dto.getCommissionRatio() != null){
            wrapper.set(Platform::getCommissionRatio,dto.getCommissionRatio());
        }
        if (dto.getPromotionUserRatio() != null){
            wrapper.set(Platform::getPromotionUserRatio,dto.getPromotionUserRatio());
        }
        if (dto.getBankCashChargesRatio() != null){
            wrapper.set(Platform::getBankCashChargesRatio,dto.getBankCashChargesRatio());
        }
        if (dto.getUserCashChargesRatio() != null){
            wrapper.set(Platform::getUserCashChargesRatio,dto.getUserCashChargesRatio());
        }
        if (dto.getCashMoney() != null){
            wrapper.set(Platform::getCashMoney,dto.getCashMoney());
        }
        baseMapper.update(wrapper);
        return true;
    }

    @Override
    public PlatformVo platformInfo(IdDto dto) {
        LambdaQueryWrapper<Platform> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Platform::getId,dto.getId());
        Platform platform = getOnly(wrapper);
        return BeanUtil.copyProperties(platform,PlatformVo.class);
    }

}